/**********************************************************************************
 * Copyright (c) 2012 - 2012,  parallel, All right reserved
 * Title: com.adc.parallel.daemon  CompleteDaemonThread.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-11-21 下午4:24:57 
 *********************************************************************************/


package com.adc.parallel.daemon;

import java.util.ArrayList;
import java.util.List;

import com.adc.parallel.constants.CompleteList;
import com.adc.parallel.constants.SysConstants;
import com.adc.parallel.constants.ThreadConstant;
import com.adc.parallel.dto.CompletedTaskDto;
import com.adc.parallel.util.LogUtil;
import com.adc.quicktools.StringUtil;


/**
 * 
 * Description: the daemon thread for complete task dto list
 * @author shen  shenqiao2012@163.com
 * @param <E>
 * @param <T>
 */
public class CompleteDaemonThread<E extends CompletedTaskDto<T>, T> extends BaseTimerThread{
	private List<E> completeList;
	private String identity;
	/**
	 * 
	* @param identity
	* @param completeList
	 */
	public CompleteDaemonThread(String identity,List<E> completeList) {
		super(ThreadConstant.getClearCompletedTime(), ThreadConstant.getClearCompletedTime());
		this.setName(SysConstants.getDaemonThreadName(identity));
		this.completeList=completeList;
		this.identity=identity;
	}

	/**
	 * Description:  
	 * @see com.adc.parallel.daemon.BaseTimerThread#operaPerCycle() 
	 */
	@Override
	public int operaPerCycle() {
		// TODO Auto-generated method stub
//		System.out.println("\t"+this.getName()+"\t is on the working!");
		int rs=ThreadConstant.EXEC_FAIL;
		if (completeList!=null&&completeList.size()>0&&StringUtil.checkStringValue(identity)) {
			if (SysConstants.ListType_CP.equals(identity)) {
				rs=writeCompletedLog(completeList, identity);
			}
		}
		return rs;
	}

	/**
	 * Description:  
	 * @see com.adc.parallel.daemon.BaseTimerThread#operaOnTimeOut() 
	 */
	@Override
	public int operaOnTimeOut() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/**
	 * 
	 * Description: write completed task info
	 * author:  shen
	 * @param completeList
	 * @param identity
	 * @return int
	 */
	private int writeCompletedLog(final List<E> completeList,String identity) {
		int rs=ThreadConstant.EXEC_FAIL;
		List<String> historiesList =new ArrayList<String>();
		for (int i = 0; i < completeList.size(); i++) {
			historiesList.add(completeList.get(i).toString());
		}
		CompleteList.removeAllDto(completeList);
		if (LogUtil.writeProcessLogPerDay(historiesList)) {
			rs=ThreadConstant.EXEC_SUCCESS;
		}
		return rs;
	}
	

}
